# ======================================================================
#
# Copyright (C) ADN
# ADN::LIBRARY::ADAPTERS::CISCO::IOS::Ios Ios.pm
# Description:
#  - Cisco IOS
#
# ======================================================================

package ADN::LIBRARY::ADAPTERS::CISCO::IOS::Ios;

use strict;
use ADN::LIBRARY::ADAPTERS::CISCO::IOS::Connect;
use ADN::LIBRARY::ADAPTERS::CISCO::IOS::Parse;
use ADN::LIBRARY::TFTP::ServerTFTP;
use ADN::TMP::TMP;

# #######################
# FCT LOG
# #######################
sub log
{
  my $self = shift;
  my ($connexion, $credential, $protocol) = @_;
  my ($con, $connect, $prompt, $log, $parse);

  $connect = ADN::LIBRARY::ADAPTERS::CISCO::IOS::Connect->new();
  ($con, $prompt) = $connect->start($connexion, $credential, $protocol, undef);
  if (defined $con) {
    if (defined $prompt && ($prompt ne '') && defined($connect->is_enable())) {
      $con->send('show log', 0);
      $log = $con->send_char('[-]*[Mm]ore[-]*', '20', $prompt, 10, 1);
      $parse = ADN::LIBRARY::ADAPTERS::CISCO::IOS::Parse->new();
      $log = $parse->log($log);
    }
    $con->disconnect();
  }
  $connect = $con = $prompt = $parse = $connexion = $credential = undef;
  return $log;
}

# #######################
# FCT BACKUP
# #######################
sub backup
{
  my $self = shift;
  my ($connexion, $credential, $protocol) = @_;
  my ($con, $connect, $prompt, $backup, $parse);

  $connect = ADN::LIBRARY::ADAPTERS::CISCO::IOS::Connect->new();
  ($con, $prompt) = $connect->start($connexion, $credential, $protocol, undef);
  if (defined $con) {
    if (defined $prompt && ($prompt ne '') && defined($connect->is_enable())) {
      $con->send('show running-config', 0);
      $backup = $con->send_char('[-]*[Mm]ore[-]*', '20', $prompt, 10, 1);
      $parse = ADN::LIBRARY::ADAPTERS::CISCO::IOS::Parse->new();
      $backup = $parse->backup($backup);
    }
    $con->disconnect();
  }
  $connect = $con = $prompt = $parse = $connexion = $credential = undef;
  return $backup;
}

# #######################
# FCT RESTORE
# #######################
sub restore
{
  my $self = shift;
  my ($connexion, $credential, $protocol, $conf) = @_;
  my ($con, $connect, $prompt, $tftp,
      $restore, $obj_temp, $file, $path);

  $connect = ADN::LIBRARY::ADAPTERS::CISCO::IOS::Connect->new();
  ($con, $prompt) = $connect->start($connexion, $credential, $protocol, undef);
  if (defined $con) {
    if (defined $prompt && ($prompt ne '') && defined($connect->is_enable())) {
       $tftp = ADN::LIBRARY::TFTP::ServerTFTP->new();
       $obj_temp = ADN::TMP::TMP->new();
       ($path, $file) = $obj_temp->start();
       if (open FILE, '>', $path.$file) {
         print FILE $conf;
         if (!close FILE) {}
         $con->send('copy tftp://'.$tftp->address_tftp().'/'.$file.' startup-config');
         $con->send_char('Destination\sfilename', '0A', '[Aa]ccessing', 1);
         $tftp->start(10);
         $restore = $tftp->boucle_send($con, '[OK]');
         $con->send('copy startup-config running-config');
         $con->send_char('Destination\sfilename', '0A', $prompt, 1);
      }
    }
    $con->disconnect();
  }
  $connect = $con = $path = $tftp = $prompt = undef;
	$obj_temp = $conf = $file = $connexion = $credential = undef;
  return $restore if ($restore ne '');
  return '-1';
}

# #######################
# FCT COMMAND
# #######################
sub command
{
  my $self = shift;
  my ($connexion, $credential, $protocol, $conf) = @_;
  my ($con, $connect, $prompt, $command, $parse);
  my (@conf);

  $connect = ADN::LIBRARY::ADAPTERS::CISCO::IOS::Connect->new();
  ($con, $prompt, $command) = $connect->start($connexion, $credential, $protocol, 1);
  if (defined $con) {
    if (defined $prompt && ($prompt ne '') && defined($connect->is_enable())) {
       $parse = ADN::LIBRARY::ADAPTERS::CISCO::IOS::Parse->new();
       @conf = $parse->command($conf);
       $command .= $con->boucle_get(\@conf, 3);
    }
    $con->disconnect();
  }
  $connect = $con = $prompt = $conf = $parse = $connexion = $credential = undef;
  return $command if ($command ne '');
  return '-1';
}
1;
__END__
